<template>
  <div class="xtx-form">
    <div class="user-info">
      <img :src="avatar" alt="" />
      <p>Hi，{{ nickname }} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input
          v-model="form.mobile"
          class="input"
          type="text"
          placeholder="绑定的手机号"
        />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input
          v-model="form.code"
          class="input"
          type="text"
          placeholder="短信验证码"
        />
        <span class="code" @click="send">{{
          countTime === 0 ? '发送验证码' : `${countTime}秒后重试`
        }}</span>
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" class="submit" @click="bind">立即绑定</a>
  </div>
</template>

<script>
import QC from 'qc'
import { ref, reactive } from 'vue'
import { useCountDown } from '@/compositions'
import { reqUserQQBindCode, reqUserQQBindLogin } from '@/api/user'
import { message } from '@/components/Message/index.js'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'

export default {
  name: 'CallbackBind',
  props: {
    openId: {
      type: String,
      default: ''
    }
  },
  setup (props) {
    // 已注册未绑定
    const form = reactive({
      mobile: '',
      code: ''
    })

    // 名称和头像
    const avatar = ref('')
    const nickname = ref('')

    if (QC.Login.check()) {
      QC.api('get_user_info').success(res => {
        avatar.value = res.data.figureurl_qq_2
        nickname.value = res.data.nickname
      })
    }

    const { start, countTime } = useCountDown(60)

    const send = () => {
      // 只有countTime为0才能调用
      if (countTime.value === 0) {
        if (/^1[3-9]\d{9}$/.test(form.mobile)) {
          // 校验通过，发送手机验证码
          reqUserQQBindCode(form.mobile).then(res => {
            start()
            console.log('开启倒计时')
          })
        } else {
          message({
            type: 'error',
            text: '请输入合法的手机号'
          })
        }
      }
    }

    // 绑定操作
    const store = useStore()
    const router = useRouter()
    const bind = async () => {
      if (!/^1[3-9]\d{9}$/.test(form.mobile) || form.code.trim() === '') return

      const res = await reqUserQQBindLogin({
        unionId: props.openId,
        mobile: form.mobile,
        code: form.code
      })
      console.log('bind res is ', res)
      // 1. 将当前返回的用户数据，通过提交mutation函数存入 vuex
      store.commit('user/setProfile', res.result)
      // 2. 跳转到首页
      router.push('/')
      // 3. 提示用户登录成功
      message({ type: 'success', text: '绑定成功' })
    }

    return {
      avatar,
      nickname,
      form,
      countTime,
      send,
      bind
    }
  }
}
</script>

<style scoped lang="less">
.user-info {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;
  img {
    background: #f2f2f2;
    width: 50px;
    height: 50px;
  }
  p {
    padding-left: 10px;
  }
}
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
